﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using Framework.Dependency;
using Microsoft.Practices.Unity;

namespace EconomicInformationServiceWcf
{
    public class UnityInstanceProvider : IInstanceProvider
    {
        private readonly Type _contractType;
        
        public UnityInstanceProvider(Type contractType)
        {
            _contractType = contractType;
        }
        
        public object GetInstance(InstanceContext instanceContext)
        {
            return DependencyManager.Container.Resolve(_contractType);
        }

        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return GetInstance(instanceContext);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
            DependencyManager.Container.Teardown(instance);
        }
    }
}